$(document).ready(function() {
	
	$("#book").change(function() {
		clearOptions("chapter");
		clearOptions("verse");
		if($(this).val() != '') {
			var formData = $("#bible").serialize();
			$.post("getChapterList.do", formData, function(data) {
				replaceOptions("chapter", data);	
			}, "json");
		}
	});
	
	$("#chapter").change(function() {
		clearOptions("verse");
		if($(this).val() != '') {
			var formData = $("#bible").serialize();
			$.post("getVerseList.do", formData, function(data) {
				replaceOptions("verse", data);	
			}, "json");
		}
	});
	
	$("#verse").change(function() {
		$("#verseText").html("");
		if($(this).val() != '') {
			var formData = $("#bible").serialize();
			$.post("getVerse.do", formData, function(data) {
				$("#verseText").html(
						data.text
						+"<b><i> -"
						+data.book
						+" "
						+data.chapter
						+":"
						+data.verse
						+"</b></i>");
			}, "json");
		}
	});
	
});

function replaceOptions(id, list) { 
	var selectElem = $('#'+id);
	$.each(list, function(opt,val) {
		selectElem.append(new Option(val, val));
	});
}

function clearOptions(id) {
	$('#'+id).empty().append('<option value="">Select</option>');
	$("#verseText").html("");
}